package edu.pku.sei.act.taskStruct;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.Map;
import java.util.TreeMap;

import org.dom4j.Element;

public class Transition extends Ident{
	protected String type;
	protected String abilityOutputId;
	protected String preTaskMessageId;
	//private Set<String> postTaskMessageIds;
	protected Set<String> preStateIds;
	protected Map<String, String> postStateConditions;
	protected Map<String, Set<String> > postTaskMessageIds;
	protected int delayTime;
	
	public void addAttrisAndEles(Element element)
	{
		addAttributes(element);
		
		Element ele;
		ele = element.addElement(TagNames.TYPE);
		ele.addText(type);
		ele = element.addElement(TagNames.ABILITY_OUTPUT_ID);
		ele.addText(abilityOutputId);
		ele = element.addElement(TagNames.PRE_TASK_MESSAGE_ID);
		ele.addText(preTaskMessageId);
				
		ele = element.addElement(TagNames.PRE_STATES);
		for(String stateId: preStateIds)
		{
			Element e;
			e = ele.addElement(TagNames.STATE_ID);
			e.addText(stateId);
		}
		ele = element.addElement(TagNames.POST_STATES);
		Set<Map.Entry<String, String>> pstates = postStateConditions.entrySet();
		for(Map.Entry<String, String> en: pstates)
		{
			Element e;
			e = ele.addElement(TagNames.STATE_ID);
			e.addText(en.getKey());
			
			e = ele.addElement(TagNames.CONDITION);
			e.addText(en.getValue());
			
			Set<String> msg_ids = postTaskMessageIds.get(en.getKey());
			for(String msg_id: msg_ids)
			{				
				e = ele.addElement(TagNames.TASK_MESSAGE_ID);
				e.addText(msg_id);
			}
		}				
	}
	
	public String getType()
	{
		return type;
	}
	
	public String getAbilityOutputId()
	{
		return abilityOutputId;
	}
	
	public String getPreTaskMessageId()
	{
		return preTaskMessageId;
	}
	
	public Set<String> getPostTaskMessageIdsById(String stateId)
	{
		return postTaskMessageIds.get(stateId);
	}
	
	public Set<String> getPreStateIds()
	{
		return preStateIds;
	}
	
	public Set<String> getPostStateIds()
	{
		return postStateConditions.keySet();
	}
	
	public String getPostStateConditionById(String id)
	{
		return postStateConditions.get(id);
	}
	
	public Transition(String id, String name)
	{
		super(id, name);
		setDefaultVal();	
	}
	
	public int getDelayTime()
	{
		return delayTime;
	}
	
	public Transition()
	{
		super();
		setDefaultVal();		
	}
	
	public Transition(Element element)
	{
		super();
		setDefaultVal();
		setIdAndName(element);
		
		Element ele;
		
		ele = element.element(TagNames.TYPE);
		if(ele != null) this.type = ele.getTextTrim();
		
		ele = element.element(TagNames.ABILITY_OUTPUT_ID);
		if(ele != null) this.abilityOutputId = ele.getTextTrim();
		
		ele = element.element(TagNames.PRE_TASK_MESSAGE_ID);
		if(ele != null) this.preTaskMessageId = ele.getTextTrim();
		
		ele = element.element(TagNames.PRE_STATES);		
		if(ele != null)
		{
			List<Element> list = ele.elements(TagNames.STATE_ID);
			for(Element e: list)
			{
				String prestate = e.getTextTrim();
				if(!"".equals(prestate))
					preStateIds.add(prestate);
			}
		}
		
		ele = element.element(TagNames.POST_STATES);
		if(ele != null)
		{
			List<Element> list = ele.elements(TagNames.POST_STATE);
			for(Element e: list)
			{				
				Element e_id = e.element(TagNames.STATE_ID);
				if(e_id != null && !"".equals(e_id.getTextTrim()))
				{					
					Element e_cond = e.element(TagNames.CONDITION);
					String cond = "";
					if(e_cond != null) cond = e_cond.getTextTrim(); 
					postStateConditions.put(e_id.getTextTrim(), cond);
					
					List<Element> eList = e.elements(TagNames.TASK_MESSAGE_ID);
					Set<String> msg_ids = new TreeSet<String>();
					for(Element e_msg: eList)
					{
						String msg_id = e_msg.getTextTrim();
						if(!"".equals(msg_id)) msg_ids.add(msg_id);							
					}
					postTaskMessageIds.put(e_id.getTextTrim(), msg_ids);
				}
			}
		}			
	}
	
	private void setDefaultVal()
	{
		type = "";
		abilityOutputId = "";
		preTaskMessageId = "";
		postTaskMessageIds = new TreeMap<String, Set<String> >();
		preStateIds = new TreeSet<String>();
		postStateConditions = new TreeMap<String, String>();	
	}
	
}
